package com.shzu.jwc.ty.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shzu.jwc.ty.bean.UserSys;
import com.shzu.jwc.ty.common.MD5Util;
import com.shzu.jwc.ty.common.Msg;
import com.shzu.jwc.ty.service.UserSysservice;

@Controller
public class LoginController {

	@Autowired
	UserSysservice userSysservice ;
	
	@RequestMapping("/login.action")
	public String login()
	{
		return "login";
	}
	
	@RequestMapping("/updatepass.action")
	public String updatepass() {
		return"updatepass";
	}
	
	@RequestMapping("/jwc/updatepass.action")
	public String jwcupdatepass() {
		return"jwc/updatepass";
	}
	
	@ResponseBody
	@RequestMapping(value="/loginfun.action",method=RequestMethod.POST)
	public Msg loginfun(@RequestParam("username") String username,@RequestParam("password") String password,HttpSession session)
	{
		if(username == null || password == null)
			return Msg.error("用户名或密码不能为空");
		UserSys userSys=userSysservice.byusernameget(username);
		if(userSys.getUsername()==null)
			return Msg.error("用户不存在");
		if(userSys.getUsername().equals(username) && userSys.getPassword().equals(MD5Util.encrypt(password))) {
			session.setAttribute("username", username);
			session.setAttribute("usertype", userSys.getUserType());
			return Msg.success("登陆成功").add("usertype", userSys.getUserType());
		}
		else
			return Msg.error("密码错误");
	}
	
	@RequestMapping("/loginout.action")
	public String loginout(HttpSession session)
	{
		session.removeAttribute("username");
		session.removeAttribute("usertype");
		return "redirect:/login.action";
	}
}
